<h2>DESCRIPTION</h2>

<em>v.in.ogr</em> imports vector data from files and database connections
supported by the <a href="https://gdal.org/">OGR</a> library) into the
current location and mapset.

<p>
If the <b>layer</b> parameter is not given, all available OGR layers
are imported as separate GRASS layers into one GRASS vector map. If
several OGR layer names are given, all these layers are imported as
separate GRASS layers into one GRASS vector map.

<p>
The optional <b>spatial</b> parameter defines spatial query extents.
This parameter allows the user to restrict the region to a spatial
subset while importing the data. All vector features completely or
partially falling into this rectangle subregion are imported.
The <b>-r</b> current region flag is identical, but uses the current
region settings as the spatial bounds
(see <em><a href="g.region.html">g.region</a></em>).

<h3>Supported Vector Formats</h3>

<em>v.in.ogr</em> uses the OGR library which supports various vector 
data formats including
<a href="https://gdal.org/drivers/vector/shapefile.html">ESRI Shapefile</a>,
<a href="https://gdal.org/drivers/vector/mitab.html">Mapinfo File</a>, UK .NTF, 
SDTS, TIGER, IHO S-57 (ENC), DGN, GML, GPX, AVCBin, REC, Memory, OGDI, 
and PostgreSQL, depending on the local OGR installation. For details 
see the <a href="https://gdal.org/drivers/vector/">OGR format overview</a>.
The <b>-f</b> prints a list of the vector formats supported 
by the system's OGR (Simple Features Library). The OGR (Simple Features 
Library) is part of the <a href="https://gdal.org">GDAL</a> library, 
hence GDAL needs to be installed to use <em>v.in.ogr</em>.

<p>
The list of actually supported formats can be printed by <b>-f</b> flag.

<h3>Topology cleaning</h3>
Topology cleaning on areas is automatically performed, but may fail in
special cases. In these cases, a <b>snap</b> threshold value is 
estimated from the imported vector data and printed out at the end. The 
vector data can then be imported again with the suggested <b>snap</b> 
threshold value which is incremented by powers of 10 until either an 
estimated upper limit for the threshold value is reached or the topology 
cleaning on areas was successful. In some cases, manual cleaning might 
be required or areas are truly overlapping, e.g. buffers created with 
non-topological software.

<p>
The <b>min_area</b> threshold value is being specified as area size in
map units with the exception of latitude-longitude locations in which
it is being specified solely in square meters.

<p>
The <b>snap</b> threshold value is used to snap boundary vertices to
each other if the distance in map units between two vertices is not
larger than the threshold. Snapping is by default disabled with
-1. See also the <em><a href="v.clean.html">v.clean</a></em> manual.

<h3>Overlapping polygons</h3>

When importing overlapping polygons, the overlapping parts will become
new areas with multiple categories, one unique category for each
original polygon. An original polygon will thus be converted to
multiple areas with the same shared category. These multiple areas
will therefore also link to the same entry in the attribute table. A
single category value may thus refer to multiple non-overlapping areas
which together represent the original polygon overlapping with another
polygon. The original polygon can be recovered by
using <em><a href="v.extract.html">v.extract</a></em> with the desired
category value or <b>where</b> statement and the <b>-d</b> flag to
dissolve common boundaries.

<h2>Location Creation</h2>

<em>v.in.ogr</em> attempts to preserve projection information when
importing datasets if the source format includes projection
information, and if the OGR driver supports it.  If the projection of
the source dataset does not match the projection of the current
location <em>v.in.ogr</em> will report an error message
(&quot;Projection of dataset does not appear to match current
location&quot;).

<p>
If the user wishes to ignore the difference between the apparent
coordinate system of the source data and the current location, they
may pass the <b>-o</b> flag to override the projection check.

<p>
If the user wishes to import the data with the full projection
definition, it is possible to have <em>v.in.ogr</em> automatically
create a new location based on the projection and extents of the file
being read. This is accomplished by passing the name to be used for
the new location via the <b>location</b> parameter.  Upon completion
of the command, a new location will have been created (with only a
PERMANENT mapset), and the vector map will have been imported with the
indicated <b>output</b> name into the PERMANENT mapset.
<p>
An interesting wrapper command around <em>v.in.ogr</em> is
<a href="v.import.html">v.import</a> which reprojects (if needed) the
vector dataset during import to the projection of the current location.

<h2>NOTES</h2>

<h3>Table column names: supported characters</h3>
The characters which are eligible for table column names are limited
by the SQL standard. Supported are:

<div class="code"><pre>
[A-Za-z][A-Za-z0-9_]*
</pre></div>

This means that SQL neither supports '.' (dots) nor '-' (minus) nor
'#' in table column names. Also a table name must start with a
character, not a number.

<p>
<em>v.in.ogr</em> converts '.', '-' and '#' to '_' (underscore) during
import. The <b>-w</b> flag changes capital column names to
lowercase characters as a convenience for SQL usage (lowercase column
names avoid the need to quote them if the attribute table is stored in
a SQL DBMS such as PostgreSQL). The <b>columns</b> parameter is used
to define new column names during import.

<p>
The DBF database specification limits column names to 10 characters.
If the default DB is set to DBF and the input data contains longer
column/field names, they will be truncated. If this results in
multiple columns with the same name then <em>v.in.ogr</em> will
produce an error.  In this case you will either have to modify the
input data or use
<em>v.in.ogr</em>'s <b>columns</b> parameter to rename columns to something
unique. (hint: copy and modify the list given with the error message).
Alternatively, change the local DB with
<em><a href="db.connect.html">db.connect</a></em>.

<h3>File encoding</h3>
When importing ESRI Shapefiles the OGR library tries to read the
LDID/codepage setting from the .dbf file and use it to translate
string fields to UTF-8. LDID &quot;87 / 0x57&quot; is treated as
ISO8859_1 which may not be appropriate for many
languages. Unfortunately it is not clear what other values may be
appropriate (see example below). To change encoding the user can set
up <tt><a href="https://gdal.org/user/configoptions.html">SHAPE_ENCODING</a></tt>
environmental variable or simply to define
encoding value using <b>encoding</b> parameter.

<p>
Value for <b>encoding</b> also affects text recoding when importing
DXF files. For other formats has encoding value no effect.

<h3>Defining the key column</h3>
Option <b>key</b> specifies the column name used for feature
categories. This column must be integer. If not specified, categories
numbers are generated starting with 1 and stored in the column named
&quot;cat&quot;.

<h3>Supports of multiple geometry columns</h3>
Starting with GDAL 1.11 the library supports multiple geometry columns
in OGR. By default <em>v.in.ogr</em> reads all geometry columns from
given layer. The user can choose desired geometry column
by <b>geometry</b> option,
see <a href="#multiple-geometry-columns">example below</a>.

<h3>Latitude-longitude data: Vector postprocessing after import</h3>
For vector data like a grid, horizontal lines need to be broken at their
intersections with vertical lines (<b>v.clean ... tool=break</b>).

<h2>EXAMPLES</h2>

The command imports various vector formats:

<h3>SHAPE files</h3>

<div class="code"><pre>
v.in.ogr input=/home/user/shape_data/test_shape.shp output=grass_map 
</pre></div>

Alternate method:

<div class="code"><pre>
v.in.ogr input=/home/user/shape_data layer=test_shape output=grass_map 
</pre></div>

Define encoding value for attribute data (in this example we expect
attribute data
in <a href="https://en.wikipedia.org/wiki/Windows-1250">Windows-1250</a>
encoding; ie. in Central/Eastern European languages that
use Latin script, Microsoft Windows encoding).

<div class="code"><pre>
v.in.ogr input=/home/user/shape_data/test_shape.shp output=grass_map encoding=cp1250
</pre></div>

<h3>MapInfo files</h3>

<div class="code"><pre>
v.in.ogr input=./ layer=mapinfo_test output=grass_map
</pre></div>

<h3>Arc Coverage</h3>

We import the Arcs and Label points, the module takes care to build
areas.

<div class="code"><pre>
v.in.ogr input=gemeinden layer=LAB,ARC type=centroid,boundary output=mymap
</pre></div>

<h3>E00 file</h3>

See also <em><a href="v.in.e00.html">v.in.e00</a></em>.

<p>
First we have to convert the E00 file to an Arc Coverage with
'avcimport'
(<a href="http://avce00.maptools.org/avce00/index.html">AVCE00
tools</a>, use <em>e00conv</em> first in case that <em>avcimport</em>
fails):

<div class="code"><pre>
avcimport e00file coverage
v.in.ogr input=coverage layer=LAB,ARC type=centroid,boundary output=mymap
</pre></div>

<h3>SDTS files</h3> 

You have to select the CATD file.

<div class="code"><pre>
v.in.ogr input=CITXCATD.DDF output=cities
</pre></div>

<h3>TIGER files</h3>

<div class="code"><pre>
v.in.ogr input=input/2000/56015/ layer=CompleteChain,PIP output=t56015_all \
type=boundary,centroid snap=-1
</pre></div>

<h3>PostGIS tables</h3>

Import polygons as areas:

<div class="code"><pre>
v.in.ogr input="PG:host=localhost dbname=postgis user=postgres" layer=polymap \
output=polygons type=boundary,centroid
</pre></div>

If the table containing the polygons are in a specific schema, you can use:

<div class="code"><pre>
v.in.ogr input="PG:host=localhost dbname=postgis user=postgres" \
layer=myschema.polymap \
output=polygons type=boundary,centroid
</pre></div>

Generally, <em>v.in.ogr</em> just follows the 
<a href="https://gdal.org/drivers/vector/">format-specific</a>
syntax defined by the OGR library.


<!-- Commented out until we clarify usage of default connection - ML
<h3>Default connection settings as datasource (PostgreSQL only)</h3>

If datasource (<b>input</b>) is specified as 'PG:' and the default DB
driver is <a href="grass-pg.html">PostgreSQL</a> (<tt>pg</tt>) than
the connection string is determined from the default DB settings, see
examples below.

<p>
For schema support, first set a default schema with 
<em><a href="db.connect.html">db.connect</a></em>. If schema support
is used the schema name must be specified whenever a <tt>db.*</tt>
module is called. User and password for connection to the database can
be specified by <em><a href="db.login.html">db.login</a></em>.

<p>
Example (with schema):

<div class="code"><pre>
db.connect driver=pg database=test schema=user1
db.login user=user1 password=pwd1
# -> input=&quot;PG:dbname=test user=user1 password=pwd1&quot; layer=&quot;user1.river&quot;
v.in.ogr input=PG: layer=river output=river     
db.select table=user1.river
</pre></div>

<p>
The user can ignore schemas, if desired:

<div class="code"><pre>
db.connect driver=pg database=test
db.login user=user1 password=pwd1
# -> input=&quot;PG:dbname=test user=user1 password=pwd1&quot;
v.in.ogr input=PG: layer=river output=river 
db.select table=river
</pre></div>
--->

<h3>OpenStreetMap (OSM)</h3>

<a href="https://gdal.org/drivers/vector/osm.html">OSM data</a> are available in
.osm (XML based) and .pbf (optimized binary) formats. The .pbf format 
is recommended because file sizes are smaller. The OSM driver will 
categorize features into 5 layers :
<ul>
    <li><b>points</b>: "node" features that have significant tags attached. 
    <li><b>lines</b>: "way" features that are recognized as non-area. 
    <li><b>multilinestrings</b>: "relation" features that form a 
multilinestring(type = 'multilinestring' or type = 'route'). 
    <li><b>multipolygons</b>: "relation" features that form a multipolygon (type 
= 'multipolygon' or type = 'boundary'), and "way" features that are 
recognized as area.
    <li><b>other_relations</b>: "relation" features that do 
not belong to any of the above layers.
</ul>

It is recommended to import one layer at a time, and to select features 
with the <b>where</b> option, e.g. to import roads, use 

<div class="code"><pre>
v.in.ogr where="highway <> ''"
</pre></div>
i.e. the OSM tag <em>highway</em> must be set.

<p>
When importing administrative boundaries from OSM, it is important to 
not only select administrative boundaries, but also the admin level to 
be imported (valid range is 1 - 11), e.g. with

<div class="code"><pre>
v.in.ogr where="boundary = 'administrative' and admin_level = '1'"
</pre></div>

<p>
The OSM topological model differs from the GRASS topological model. OSM 
topologically correct connections of lines can be on all vertices of a 
line. During import, lines are automatically split at those vertices 
where an OSM connection to another line exists.

<p>
Import of OSM data requires a configuration file, defined with the 
OSM_CONFIG_FILE configuration option. In the data folder of the GDAL 
distribution, you can find a <a href="https://github.com/OSGeo/gdal/blob/master/gdal/data/osmconf.ini">osmconf.ini file</a>
that can be customized to fit your needs. See 
<a href="https://wiki.openstreetmap.org/wiki/Map_Features">OSM map features</a> 
for keys and their values. You should set &quot;other_tags=no&quot; to 
avoid problems with import or querying the imported vector. Once a 
OSM_CONFIG_FILE has been created, OSM data can be imported with e.g.

<div class="code"><pre>
export OSM_CONFIG_FILE=/path/to/osmconf.ini
v.in.ogr input=name.pbf layer=lines output=osm_data
</pre></div>

<h3>Oracle Spatial</h3>

Note that you have to set the environment-variables <tt>ORACLE_BASE,
ORACLE_SID, ORACLE_HOME</tt> and <tt>TNS_ADMIN</tt> accordingly.

<div class="code"><pre>
v.in.ogr input=OCI:username/password@database_instance output=grasslayer layer=roads_oci
</pre></div>

<h3>Multiple geometry columns</h3>

This example shows how to work with data which contain multiple
geometry per feature. The number of geometry columns per feature can
be checked by <em><a href="v.external.html">v.external</a></em>
together with <b>-t</b> flag.
                  
<div class="code"><pre>
v.external -t input=20141130_ST_UKSH.xml.gz
...
Okresy,point,1,DefinicniBod
Okresy,multipolygon,1,OriginalniHranice
Okresy,multipolygon,1,GeneralizovaneHranice
...
</pre></div>      

In our example layer &quot;Okresy&quot; has three geometry columns:
&quot;DefinicniBod&quot;, &quot;OriginalniHranice&quot; and
&quot;GeneralizovanaHranice&quot;. By default <em>v.in.ogr</em> reads data from
all three geometry columns. The user can specify desired geometry
column by <b>geometry</b> option, in this case the module will read
geometry only from the specified geometry column. In the example below,
the output vector map will contain only geometry saved in
&quot;OriginalniHranice&quot; geometry column.

<div class="code"><pre>
v.in.ogr input=20141130_ST_UKSH.xml.gz layer=Okresy geometry=OriginalniHranice
</pre></div>      

<h2>WARNINGS</h2>

If a message like

<div class="code"><pre>
WARNING: Area size 1.3e-06, area not imported
</pre></div>

appears, the <b>min_area</b> may be adjusted to a
smaller value so that all areas are imported. Otherwise tiny areas are
filtered out during import (useful to polish digitization errors or
non-topological data).

<p>
If a message like

<div class="code"><pre>
Try to import again, snapping with at least 1e-008: 'snap=1e-008'
</pre></div>
appears, then the map to be imported
contains topological errors. The message suggests a value for the
<em>snap</em> parameter to be tried. For more details, see above in
<em><a href="v.in.ogr.html#topology-cleaning">Topology Cleaning</a></em>.

<h2>ERROR MESSAGES</h2>

<h3>SQL syntax errors</h3>

Depending on the currently selected SQL driver, error messages such as follows may arise:

<div class="code"><pre>
DBMI-SQLite driver error:
Error in sqlite3_prepare():
near "ORDER": syntax error
</pre></div>

Or:

<div class="code"><pre>
DBMI-DBF driver error:
SQL parser error:
syntax error, unexpected DESC, expecting NAME processing 'DESC
</pre></div>

This indicates that a column name in the input dataset corresponds to a reserved
SQL word (here: 'ORDER' and 'DESC' respectively). A different column name has to be
used in this case. The <b>columns</b> parameter can be used to assign different
column names on the fly in order to avoid using reserved SQL words.

For a list of SQL reserved words for SQLite (the default driver),
see <a href="https://www.sqlite.org/lang_keywords.html">here</a>.

<h3>Projection errors</h3>

<div class="code"><pre>
Projection of dataset does not appear to match the current location.
</pre></div>

Here you need to create or use a location whose projection matches that
of the vector data you wish to import. Try using <b>location</b> parameter to
create a new location based upon the projection information in the file. If
desired, you can then re-project it to another location
with <em><a href="v.proj.html">v.proj</a></em>.

<h2>REFERENCES</h2>

<ul>
  <li><a href="https://gdal.org/">OGR vector library</a></li>
  <li><a href="https://gdal.org/api/vector_c_api.html">OGR vector library C API</a> documentation</li>
</ul>

<h2>SEE ALSO</h2>

<em>
<a href="db.connect.html">db.connect</a>,
<a href="v.clean.html">v.clean</a>,
<a href="v.extract.html">v.extract</a>,
<a href="v.build.polylines.html">v.build.polylines</a>,
<a href="v.edit.html">v.edit</a>,
<a href="v.external.html">v.external</a>,
<a href="v.import.html">v.import</a>,
<a href="v.in.db.html">v.in.db</a>,
<a href="v.in.e00.html">v.in.e00</a>,
<a href="v.out.ogr.html">v.out.ogr</a>
</em>

<p>
GRASS GIS Wiki page: Import of <a href="https://grasswiki.osgeo.org/wiki/Global_datasets">Global datasets</a>

<h2>AUTHORS</h2>

Original author: Radim Blazek, ITC-irst, Trento, Italy
<br>
Location and spatial extent support by Markus Neteler and Paul Kelly
<br>
Various improvements by Markus Metz
<br>
Multiple geometry columns support by Martin Landa, OSGeoREL, Czech Technical University in Prague, Czech Republic

<!--
<p>
<i>Last changed: $Date$</i>
-->
